package com.awesome.uidemo.eventdispatch;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.awesome.common.util.log.ShowLogUtil;

/**
 * Author: JfangZ
 * Email: zhangjingfang@jeejio.com
 * Date: 2021/4/25 14:46
 * Description:
 */
public class MyTextView extends TextView {
    public MyTextView(Context context) {
        super(context);
    }

    public MyTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                ShowLogUtil.info("View dispatchTouchEvent ACTION_DOWN");
//                // 父View的onTouchEvent
//                return false;
//                // 消费
//                return true;
                break;
            case MotionEvent.ACTION_MOVE:
                ShowLogUtil.info("View dispatchTouchEvent ACTION_MOVE");
                break;
            case MotionEvent.ACTION_UP:
                ShowLogUtil.info("View dispatchTouchEvent ACTION_UP");
                break;
        }
        // 子View的dispatchTouchEvent
        boolean dispatchTouchEvent = super.dispatchTouchEvent(ev);
        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                ShowLogUtil.info("---return View dispatchTouchEvent ACTION_DOWN "+dispatchTouchEvent);
                break;
            case MotionEvent.ACTION_MOVE:
                ShowLogUtil.info("---return View dispatchTouchEvent ACTION_MOVE "+dispatchTouchEvent);
                break;
            case MotionEvent.ACTION_UP:
                ShowLogUtil.info("---return View dispatchTouchEvent ACTION_UP "+dispatchTouchEvent);
                break;
        }
        return dispatchTouchEvent;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                ShowLogUtil.info("View onTouchEvent ACTION_DOWN");
//                // 父View的onTouchEvent
//                return false;
//                // 消费
                return true;
//                break;
            case MotionEvent.ACTION_MOVE:
                ShowLogUtil.info("View onTouchEvent ACTION_MOVE");
                break;
            case MotionEvent.ACTION_UP:
                ShowLogUtil.info("View onTouchEvent ACTION_UP");
                break;
            case MotionEvent.ACTION_CANCEL:
                ShowLogUtil.info("View onTouchEvent ACTION_CANCEL");
                break;
        }
        // 父View的onTouchEvent
        boolean onTouchEvent = super.onTouchEvent(event);
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                ShowLogUtil.info("---return View onTouchEvent ACTION_DOWN "+onTouchEvent);
                break;
            case MotionEvent.ACTION_MOVE:
                ShowLogUtil.info("---return View onTouchEvent ACTION_MOVE "+onTouchEvent);
                break;
            case MotionEvent.ACTION_UP:
                ShowLogUtil.info("---return View onTouchEvent ACTION_UP "+onTouchEvent);
                break;
            case MotionEvent.ACTION_CANCEL:
                ShowLogUtil.info("---return View onTouchEvent ACTION_CANCEL "+onTouchEvent);
                break;
        }
        return onTouchEvent;
    }
}
